package com.example.datadashboard.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.datadashboard.entity.Widget;
import com.example.datadashboard.mapper.WidgetMapper;
import com.example.datadashboard.service.WidgetService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class WidgetServiceImpl extends ServiceImpl<WidgetMapper, Widget> implements WidgetService {
    @Override
    public List<Widget> getDashboardWidgets(Long dashboardId) {
        QueryWrapper<Widget> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("dashboard_id", dashboardId);
        return baseMapper.selectList(queryWrapper);
    }

    @Override
    @Transactional
    public void updateWidgetPosition(Long widgetId, Integer x, Integer y, Integer width, Integer height) {
        Widget widget = getById(widgetId);
        widget.setPositionX(x);
        widget.setPositionY(y);
        widget.setWidth(width);
        widget.setHeight(height);
        updateById(widget);
    }
}
